package com.admin_springboot.adminspringboot.service;

import com.admin_springboot.adminspringboot.entity.User;
import com.admin_springboot.adminspringboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fan
 */

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 新增用户
     * @param user user对象
     * @return 受影响行数
     */
    public int saveUser(User user){
        return userMapper.insert(user);
    }

    /**
     * 更新用户
     * @param user user对象
     * @return 受影响行数
     */
    public int editUSer(User user){
        return userMapper.update(user);
    }

    /**
     * 删除用户
     * @param id 用户id
     * @return
     */
    public int deleteUser(int id){
        return userMapper.deleteById(id);
    }

    /**
     * 分页查询用户
     * @param pageNum 页数
     * @param pageSize 每页显示大小
     * @param username 用户名
     * @return
     */
    public Map selectByPage( Integer pageNum ,Integer pageSize, String username, String phone){
        pageNum =(pageNum -1)*pageSize;
        Integer total= userMapper.selectTotal();//查询总条数
        List<User> data =userMapper.selectPage(pageNum,pageSize,username,phone);//分页查询数据
        Map<String,Object> res =new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }
}

